<template>
  <section class="formHeight">
    <el-form :model="fromData" :rules="fromData_rules" ref="fromDataRef" label-width="140px">

      <el-form-item label="单元类型" v-if="!set" prop="type">
        <el-select class="w100" v-model="fromData.type" placeholder="请选择单元类型" @change="typeChange">
          <el-option v-for="(item,index) in unitType" v-show="index!=0" :key="index" :label="item.name" :value="item.type"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单元类型" v-else prop="type">
        <el-input :readonly="true" v-model="unitType[fromData.type].name"></el-input>
      </el-form-item>
      <el-form-item label="单元名称" prop="name">
        <el-input :readonly="_readonly" v-model="fromData.name" @input="nameInput" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="仪器类型" prop="mode">
        <el-select class="w100" v-model="fromData.mode" placeholder="仪器类型">
          <el-option v-for="item in instrumentType" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item v-if="!set" label="检测间隔(天)" prop="measurementInterval">
        <el-select class="w100" v-model="fromData.measurementInterval" @change="measurementChange" placeholder="检测间隔">
          <el-option v-for="item in measurementIntervalOptions" :key="item.value" :label="item.name" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="检测间隔(天)" prop="measurementInterval">
        <el-select class="w100" v-model="fromData.measurementInterval" @change="measurementChange" placeholder="检测间隔">
          <el-option v-for="item in measurementIntervalOptions" :key="item.value" :label="item.name" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检测频率(次)" prop="frequency">
        <el-input type="number" v-if="fromData.measurementInterval=='1'" placeholder="请输入检测频率" :readonly="_readonly" v-model="fromData.frequency"></el-input>
        <el-input type="number" v-else :readonly="true" placeholder="请输入检测频率" v-model="fromData.frequency"></el-input>
      </el-form-item>
      <el-form-item label="安装时间" prop="installationTime">
        <el-date-picker class="w100" value-format="yyyy-MM-dd HH:mm:ss" v-model="fromData.installationTime" type="datetime" placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="首测时间" prop="startTime">
        <el-date-picker class="w100" :disabled="set" value-format="yyyy-MM-dd HH:mm:ss" v-model="fromData.startTime" type="datetime" placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="位移基准值(mm)" prop="displacementReferenceValue">
        <!-- <el-input type="number" :readonly="_readonly" placeholder="请输入位移基准值(mm)" @input="item=>{fromData.displacementReferenceValue=(fixed_1(item))}" v-model.number="fromData.displacementReferenceValue"></el-input> -->
        <el-input type="number" :readonly="_readonly" placeholder="请输入位移基准值(mm)" v-model.number="fromData.displacementReferenceValue"></el-input>
      </el-form-item>
      <el-form-item label="变形速率(mm/天)" prop="reservedRate">
        <!-- <el-input type="number" :readonly="_readonly" placeholder="请输入变形速率(mm/天)" @input="item=>{fromData.reservedRate=(fixed_1(item))}" v-model.number="fromData.reservedRate"></el-input> -->
        <el-input type="number" :readonly="_readonly" placeholder="请输入变形速率(mm/天)" v-model.number="fromData.reservedRate"></el-input>
      </el-form-item>
      <el-form-item v-if="set" label="测量状态" prop="flag">
        <el-input :readonly="true" :value="flagOptions[fromData.flag-0].name"></el-input>
      </el-form-item>
      <!-- <el-form-item v-if="!set" label="新建数量" prop="num">
        <el-input type="number" :min="1" :max="99" @input="numberChange" :readonly="_readonly" v-model="fromData.num"></el-input>
      </el-form-item> -->

      <el-form-item label="安装人" prop="installationPersonnel">
        <el-input :readonly="_readonly" placeholder="请输入安装人" v-model="fromData.installationPersonnel"></el-input>
      </el-form-item>
      <!-- <el-form-item label="安装人" prop="installationPersonnel">
      <iSelectedStaff :defaultSelect="catchArray.installationPersonnelData" @selectChange="azr_change_fun">
        <el-input :readonly="true" v-model="catchArray.installationPersonnelData.name"></el-input>
      </iSelectedStaff>
    </el-form-item> -->
      <el-form-item label="维护人" prop="maintenancePersonnel">
        <el-input :readonly="_readonly" placeholder="请输入维护人" v-model="fromData.maintenancePersonnel"></el-input>
      </el-form-item>
      <!-- <el-form-item label="维护人" prop="maintenancePersonnel">
      <iSelectedStaff :defaultSelect="catchArray.maintenancePersonnelData" @selectChange="whr_change_fun">
        <el-input :readonly="true" v-model="catchArray.maintenancePersonnelData.name"></el-input>
      </iSelectedStaff>
    </el-form-item> -->
      <el-form-item label="施工工况" prop="constructionState">
        <el-input :readonly="_readonly" placeholder="请输入施工工况" v-model="fromData.constructionState"></el-input>
      </el-form-item>
      <el-form-item label="埋设位置" prop="embeddingLocation">
        <el-input :readonly="_readonly" placeholder="请输入埋设位置" v-model="fromData.embeddingLocation"></el-input>
      </el-form-item>
      <el-form-item label="依据标准" prop="standard">
        <el-input :readonly="_readonly" placeholder="请输入依据标准" v-model="fromData.standard"></el-input>
      </el-form-item>
      <!-- <el-form-item label="环境条件" prop="surroundings">
        <el-input :readonly="_readonly" placeholder="请输入环境条件" v-model="fromData.surroundings"></el-input>
      </el-form-item> -->
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" placeholder="请输入描述" :readonly="_readonly" v-model="fromData.remark"></el-input>
      </el-form-item>
      <el-form-item v-if="!set">
        <el-switch v-model="addPoint" :disabled="addPointDisabled" inactive-text="创建测点">
        </el-switch>
      </el-form-item>
      <!-- 创建点↓ -->
      <section v-if="!set&&addPoint">
        <el-divider></el-divider>
        <el-form-item label="点名称前缀" prop="pointPrefix">
          <el-input :readonly="_readonly" placeholder="请输入半生点位的名称前缀" v-model="fromData.pointPrefix"></el-input>
        </el-form-item>
        <el-form-item v-if="!set" label="新建数量" prop="pointNum">
          <el-input type="number" :min="1" :max="99" @input="numberChange2" :disabled="pointNumDisabled" :readonly="_readonly" v-model="fromData.pointNum"></el-input>
        </el-form-item>
        <el-form-item v-if="!set" label="起始编号" prop="startNumber">
          <el-input type="number" :readonly="_readonly" v-model="fromData.startNumber"></el-input>
        </el-form-item>
      </section>
    </el-form>

  </section>
</template>

<script>
import * as api from '../../../api/private/unit'
import { positiveInteger } from '../../../utils/validate'
export default {
  props: {
    fromData: {},
    _readonly: {
      type: Boolean,
      default: false
    },
    set: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      fromData_rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'change' }],
        type: [{ required: true, message: '请选择类型', trigger: 'change' }],
        mode: [{ required: true, message: '请选择仪器类型', trigger: 'change' }],
        frequency: [
          { required: true, message: '请设置检测频率', trigger: 'change' },
          { validator: positiveInteger, trigger: 'change' }
        ],
        measurementInterval: [{ required: true, message: '请设置检测间隔', trigger: 'change' }],
        startTime: [{ required: true, message: '请设置首测时间', trigger: 'change' }],
        installationTime: [{ required: true, message: '请设置首测时间', trigger: 'change' }],
        pointPrefix: [{ required: true, message: '请设置测点前缀', trigger: 'change' }],
        pointNum: [{ required: true, message: '请设置测点数量', trigger: 'change' }],
        startNumber: [{ required: true, message: '请设置测点起始编号', trigger: 'change' }],
        displacementReferenceValue: [{ required: true, message: '请设置位移基准值', trigger: 'change' }],
        reservedRate: [{ required: true, message: '请设置变形速率', trigger: 'change' }]
      },

      unitType: [],
      // 选中数据缓存
      catchArray: {
        installationPersonnelData: {},
        maintenancePersonnelData: {}
      },
      measurementIntervalOptions: [
        { name: '1天', value: '1' },
        { name: '2天', value: '2' },
        { name: '3天', value: '3' },
        { name: '5天', value: '5' },
        { name: '7天', value: '7' }
      ],
      flagOptions: [
        { name: '无', value: '0' },
        { name: '待测量', value: '1' },
        { name: '测量中', value: '2' },
        { name: '测量完成', value: '3' }
      ],
      addPointDisabled: false,
      addPoint: false,
      pointNumDisabled: false,

      instrumentType: [
        { name: '全站仪', id: 1 },
        { name: '水准仪', id: 2 },
        { name: '收敛计', id: 3 }
      ]
    }
  },
  created() {
    this.unitType = this.$store.state.unit
    // this.getInstrumentType()
  },
  methods: {
    async getInstrumentType() {
      let req = {}
      let res = await api.getInstrumentTypeLint(req)
      if (res.code == 0) {
        this.instrumentType = res.data
      } else {
        this.$msg.error(res.msg)
      }
    },
    nameInput(str) {
      this.fromData.pointPrefix = str
      return str
    },
    typeChange() {
      let startMileage = JSON.parse(window.localStorage.getItem('startMileage'))
      let form = this.fromData
      let type = this.fromData.type
      switch (type) {
        case 1:
          form.name = `GDXC${startMileage}`
          form.pointPrefix = `GDXC${startMileage}`
          this.addPoint = true
          this.addPointDisabled = true
          this.pointNumDisabled = true
          this.fromData.pointNum = 1
          break
        case 2:
          form.name = `JKSL${startMileage}`
          form.pointPrefix = `JKSL${startMileage}`
          this.addPoint = true
          this.addPointDisabled = true
          this.pointNumDisabled = true
          this.fromData.pointNum = 2
          break
        case 3:
          form.name = `DBXC${startMileage}`
          form.pointPrefix = `DBXC${startMileage}`
          this.addPoint = false
          this.addPointDisabled = false
          this.pointNumDisabled = false
          this.fromData.pointNum = 0
          break
        case 4:
          form.name = `JKSCB${startMileage}`
          form.pointPrefix = `JKSCB${startMileage}`
          this.addPoint = true
          this.addPointDisabled = true
          this.pointNumDisabled = true
          this.fromData.pointNum = 4
          break
        default:
      }
    },
    validate() {
      let res_bool = false
      this.$refs.fromDataRef.validate(bool => {
        res_bool = bool
      })
      return res_bool
    },
    resetFields() {
      this.$refs.fromDataRef.resetFields()
    },
    // 安装人选择改变
    azr_change_fun(selected) {
      this.fromData.installationPersonnel = selected.id
      this.catchArray.installationPersonnelData = selected
    },
    // 维护人选择改变
    whr_change_fun(selected) {
      this.fromData.maintenancePersonnel = selected.id
      this.catchArray.maintenancePersonnelData = selected
    },
    // 检测间隔选中项改变
    measurementChange(item) {
      if (item != '1') {
        this.fromData.frequency = '1'
      }
    },
    numberChange(val) {
      if (val - 0 > 99) {
        this.fromData.num = 99
      }
      if (val - 0 < 0) {
        this.fromData.num = 0
      }
    },
    numberChange2(val) {
      if (val - 0 > 99) {
        this.fromData.pointNum = 99
      }
      if (val - 0 < 0) {
        this.fromData.pointNum = 0
      }
    }
  }
}
</script>


<style lang="scss" scoped>
.formHeight {
  max-height: 50vh;
  overflow-y: auto;
  padding-right: 10px;
}
</style>